<!--
 * @author luguoxiang
 * @date 2022/9/17
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
-->
<template>
  <edit-container ref="containerRef" v-bind="containerConfig">
    <lg-form ref="formRef" v-loading="loading" :schema="schema" v-bind="formConfig">
      <template #expiresTime>
        <t-select v-model="expiresTimeType" style="width: 150px">
          <t-option :value="0" label="永久" />
          <t-option :value="1" label="固定值" />
        </t-select>
        <div class="gl-pl"></div>
        <t-date-picker v-if="expiresTimeType === 1" v-model="data.expiresTime" enable-time-picker class="gl-flex-1" />
      </template>
      <template #maxUser>
        <t-select v-model="maxUserType" style="width: 150px">
          <t-option :value="0" label="不限" />
          <t-option :value="1" label="固定值" />
        </t-select>
        <div class="gl-pl"></div>
        <t-input-number
          v-if="maxUserType === 1"
          v-model="data.maxUser"
          enable-time-picker
          :min="1"
          step="1"
          class="gl-flex-1"
        />
      </template>
      <template #maxOnline>
        <t-select v-model="maxOnlineType" style="width: 150px">
          <t-option :value="0" label="不限" />
          <t-option :value="1" label="固定值" />
        </t-select>
        <div class="gl-pl"></div>
        <t-input-number
          v-if="maxOnlineType === 1"
          v-model="data.maxOnline"
          enable-time-picker
          :min="1"
          step="1"
          class="gl-flex-1"
        />
      </template>
    </lg-form>
  </edit-container>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { EditContainer, useEditContainer } from '@/components';
import { LgForm } from '@/soar';
import schema from './schema';
import api from './api';

const emits = defineEmits(['ok']);

const containerRef = ref();
const formRef = ref();

const expiresTimeType = ref(0);
const maxUserType = ref(0);
const maxOnlineType = ref(0);

const { containerConfig, formConfig, loading, open, data } = useEditContainer<any>({
  header: '租户',
  containerRef,
  formRef,
  getData: (params: any) => {
    return api.detail(params.id).then((res: any) => {
      expiresTimeType.value = res.expiresTime ? 1 : 0;
      maxUserType.value = res.maxUser ? 1 : 0;
      maxOnlineType.value = res.maxOnline ? 1 : 0;
      return res;
    });
  },
  onOk: async ({ values, data, params, isUpdate, changed }) => {
    if (changed) {
      values.id = data.id;
      values.expiresTime = expiresTimeType.value ? values.expiresTime : null;
      values.maxUser = maxUserType.value ? values.maxUser : 0;
      values.maxOnline = maxOnlineType.value ? values.maxOnline : 0;
      await api.saveOrUpdate(values, isUpdate);
      emits('ok', values);
    }
  },
  containerProps: {
    width: '600px',
  },
  data: { type: 1, status: 0 },
});

defineExpose({ open });
</script>
